import os
import allure
import pyautogui    
from selenium.webdriver.remote.webdriver import WebDriver
from psa.log import logger



class BaseBrowser:

    def __init__(self, driver: WebDriver):
        self.driver = driver

    def get(self, url: str, name="Get URL"):
        
        @allure.step('Get url {}'.format(url))
        def get_url():
            self.driver.get(url)
            logger.info('Get url [{}]'.format(url))
        get_url()

    def back(self):
        @allure.step("Browser Back To Prev Page")
        def back_step():
            self.driver.back()
        back_step()

    def refresh(self):
        @allure.step("Browser Refresh Page")
        def refresh_step():
            self.driver.refresh()
        refresh_step()
        
    def get_title(self) -> str:
        @allure.step('Get title [{}]'.format(self.driver.title))
        def get_title_step():
            logger.info('Get title [{}]'.format(self.driver.title)) 
            return self.driver.title    
        return get_title_step()
    
    def get_current_url(self) -> str:
        url = self.driver.current_url
        @allure.step('Get current url [{}]'.format(url))
        def get_current_url_step(url):
            logger.info('Get current url [{}]'.format(url)) 
            return url
        return get_current_url_step(url)
        
    
    def get_html_source(self):
        @allure.step('Get html source')
        def get_html_source():
            logger.info('Get html source')
            return self.driver.page_source
        return get_html_source()
    
    def maximize_window(self):
        @allure.step('Maximize window')
        def maximize_window():
            logger.info('Maximize window')
            self.driver.maximize_window()
        maximize_window()
    
    def minimize_window(self):
        @allure.step('Minimize Window')
        def minimize_window():
            logger.info('Minimize Window')
            self.driver.minimize_window()
        minimize_window()

    def screenshot(self, save_path, name="screenshot"):
        @allure.step('Browser Screenshot')
        def do_screenshot(save_path, name):
            self.driver.save_screenshot(save_path)
            logger.info('{} Success'.format(name))
        do_screenshot(save_path, name)

    def system_screenshot(self, save_path: str, name="system screenshot"):
        @allure.step('System GUI Screenshot')
        def full_screenshot():
            pyautogui.screenshot(save_path)
            logger.info('{} Success'.format(name))
        full_screenshot()
    
    def close_browser(self):
        @allure.step('Close Browser')
        def close():
            self.driver.quit()
            logger.info('Close Browser')
        close()